#! /usr/bin/perl
#                              -*- Mode: Cperl -*- 
# image.postrm --- 
# Author           : Manoj Srivastava ( srivasta@glaurung.green-gryphon.com ) 
# Created On       : Sat May 15 11:05:13 1999
# Created On Node  : glaurung.green-gryphon.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Wed Sep 13 11:26:19 2006
# Last Machine Used: glaurung.internal.golden-gryphon.com
# Update Count     : 57
# Status           : Unknown, Use with caution!
# HISTORY          : 
# Description      : 
# 
#     $Id: image.postrm,v 1.31 2003/10/07 16:24:20 srivasta Exp $
#


# 
#use strict; #for debugging
use Cwd 'abs_path';

$|=1;

# Predefined values:
my $version           = "=V";
my $link_in_boot      = "";	# Should be empty, mostly
my $no_symlink        = "";	# Should be empty, mostly
my $reverse_symlink   = "";	# Should be empty, mostly
my $do_symlink        = "Yes";	# target machine defined
my $do_boot_enable    = "Yes";	# target machine defined
my $do_bootfloppy     = "Yes";	# target machine defined
my $do_bootloader     = "Yes";	# target machine defined
my $move_image        = '';       # target machine defined
my $kimage            = "=K";	# Should be empty, mostly
my $loader            = "=L";     # lilo, silo, quik, palo, vmelilo, or nettrom
my $image_dir         = "/boot";     # where the image is located
my $clobber_modules   = '';       # target machine defined
my $initrd            = "YES";     # initrd kernel
my $do_initrd         = '';       # Normally, we don't
my $warn_initrd       = 'YES';    # Normally we do
my $use_hard_links    = '';       # hardlinks do not work across fs boundaries
my $postinst_hook     = '';       #Normally we do not
my $postrm_hook       = '';       #Normally we do not
my $preinst_hook      = '';       #Normally we do not
my $prerm_hook        = '';       #Normally we do not
my $minimal_swap      = '';       # Do not swap symlinks
my $ignore_depmod_err = '';	# normally we do not
my $relink_build_link = 'YES';	# There is no harm in checking the link
my $force_build_link  = '';	# we shall not create a dangling link
my $kernel_arch       = "=B";
my $ramdisk           = "/usr/sbin/update-initramfs";
my $package_name    = "linux-image-$version";

my $Loader          = "NoLOADER"; # 
$Loader             = "LILO"     if $loader =~ /^lilo/io;
$Loader             = "SILO"     if $loader =~ /^silo/io;
$Loader             = "QUIK"     if $loader =~ /^quik/io;
$Loader             = "yaboot"   if $loader =~ /^yaboot/io;
$Loader             = "PALO"     if $loader =~ /^palo/io;
$Loader             = "NETTROM"  if $loader =~ /^nettrom/io;
$Loader             = "VMELILO"  if $loader =~ /^vmelilo/io;
$Loader             = "ZIPL"     if $loader =~ /^zipl/io;
$Loader             = "ELILO"    if $loader =~ /^elilo/io;


# This should not point to /tmp, because of security risks.
my $temp_file_name = "/var/log/$loader" . "_log.$$";

#known variables
my @boilerplate     = ();
my @silotemplate    = ();
my @quiktemplate    = ();
my @palotemplate    = ();
my @vmelilotemplate = ();
my $bootdevice      = '';
my $rootdevice      = '';
my $rootdisk        = '';
my $rootpartition   = '';
my $image_dest      = "/";
my $realimageloc    = "/$image_dir/";
my $have_conffile   = "";
my $CONF_LOC        = '/etc/kernel-img.conf';
my $relative_links = '';
my $silent_modules  = '';
my $silent_loader   = '';
my $warn_reboot     = 'Yes';     # Warn that we are installing a version of
                                 # the kernel we are running

chdir('/')           or die "could not chdir to /:$!\n";
# remove multiple leading slashes; make sure there is at least one.
$realimageloc  =~ s|^/*|/|o;
$realimageloc  =~ s|/+|/|o;


if (-r "$CONF_LOC" && -f "$CONF_LOC"  ) {
  if (open(CONF, "$CONF_LOC")) {
    while (<CONF>) {
      chomp;
      s/\#.*$//g;
      next if /^\s*$/;

      $do_symlink      = "" if /^\s*do_symlinks\s*=\s*(no|false|0)\s*$/ig;
      $no_symlink      = "" if /^\s*no_symlinks\s*=\s*(no|false|0)\s*$/ig;
      $reverse_symlink = "" if /^\s*reverse_symlinks\s*=\s*(no|false|0)\s*$/ig;
      $link_in_boot    = "" if /^\s*image_in_boot\s*=\s*(no|false|0)\s*$/ig;
      $link_in_boot    = "" if /^\s*link_in_boot\s*=\s*(no|false|0)\s*$/ig;
      $move_image      = "" if /^\s*move_image\s*=\s*(no|false|0)\s*$/ig;
      $clobber_modules = '' if /^\s*clobber_modules\s*=\s*(no|false|0)\s*$/ig;
      $do_boot_enable  = '' if /^\s*do_boot_enable\s*=\s*(no|false|0)\s*$/ig;
      $do_bootfloppy   = '' if /^\s*do_bootfloppy\s*=\s*(no|false|0)\s*$/ig;
      $relative_links  = '' if /^\s*relative_links \s*=\s*(no|false|0)\s*$/ig;
      $do_bootloader   = '' if /^\s*do_bootloader\s*=\s*(no|false|0)\s*$/ig;
      $do_initrd       = '' if /^\s*do_initrd\s*=\s*(no|false|0)\s*$/ig;
      $warn_initrd     = '' if /^\s*warn_initrd\s*=\s*(no|false|0)\s*$/ig;
      $use_hard_links  = '' if /^\s*use_hard_links\s*=\s*(no|false|0)\s*$/ig;
      $silent_modules  = '' if /^\s*silent_modules\s*=\s*(no|false|0)\s*$/ig;
      $silent_loader   = '' if /^\s*silent_loader\s*=\s*(no|false|0)\s*$/ig;
      $warn_reboot     = '' if /^\s*warn_reboot\s*=\s*(no|false|0)\s*$/ig;
      $minimal_swap    = '' if /^\s*minimal_swap\s*=\s*(no|false|0)\s*$/ig;
      $ignore_depmod_err = '' if /^\s*ignore_depmod_err\s*=\s*(no|false|0)\s*$/ig;
      $relink_build_link = '' if /^\s*relink_build_link\s*=\s*(no|false|0)\s*$/ig;
      $force_build_link = '' if /^\s*force_build_link\s*=\s*(no|false|0)\s*$/ig;

      $do_symlink      = "Yes" if /^\s*do_symlinks\s*=\s*(yes|true|1)\s*$/ig;
      $no_symlink      = "Yes" if /^\s*no_symlinks\s*=\s*(yes|true|1)\s*$/ig;
      $reverse_symlink = "Yes" if /^\s*reverse_symlinks\s*=\s*(yes|true|1)\s*$/ig;
      $link_in_boot    = "Yes" if /^\s*image_in_boot\s*=\s*(yes|true|1)\s*$/ig;
      $link_in_boot    = "Yes" if /^\s*link_in_boot\s*=\s*(yes|true|1)\s*$/ig;
      $move_image      = "Yes" if /^\s*move_image\s*=\s*(yes|true|1)\s*$/ig;
      $clobber_modules = "Yes" if /^\s*clobber_modules\s*=\s*(yes|true|1)\s*$/ig;
      $do_boot_enable  = "Yes" if /^\s*do_boot_enable\s*=\s*(yes|true|1)\s*$/ig;
      $do_bootfloppy   = "Yes" if /^\s*do_bootfloppy\s*=\s*(yes|true|1)\s*$/ig;
      $do_bootloader   = "Yes" if /^\s*do_bootloader\s*=\s*(yes|true|1)\s*$/ig;
      $relative_links  = "Yes" if /^\s*relative_links\s*=\s*(yes|true|1)\s*$/ig;
      $do_initrd       = "Yes" if /^\s*do_initrd\s*=\s*(yes|true|1)\s*$/ig;
      $warn_initrd     = "Yes" if /^\s*warn_initrd\s*=\s*(yes|true|1)\s*$/ig;
      $use_hard_links  = "Yes" if /^\s*use_hard_links\s*=\s*(yes|true|1)\s*$/ig;
      $silent_modules  = 'Yes' if /^\s*silent_modules\s*=\s*(yes|true|1)\s*$/ig;
      $silent_loader   = 'Yes' if /^\s*silent_loader\s*=\s*(yes|true|1)\s*$/ig;
      $warn_reboot     = 'Yes' if /^\s*warn_reboot\s*=\s*(yes|true|1)\s*$/ig;
      $minimal_swap    = 'Yes' if /^\s*minimal_swap\s*=\s*(yes|true|1)\s*$/ig;
      $ignore_depmod_err = 'Yes' if /^\s*ignore_depmod_err\s*=\s*(yes|true|1)\s*$/ig;
      $relink_build_link = 'Yes' if /^\s*relink_build_link\s*=\s*(yes|true|1)\s*$/ig;
      $force_build_link = 'Yes' if /^\s*force_build_link\s*=\s*(yes|true|1)\s*$/ig;

      $image_dest      = "$1"  if /^\s*image_dest\s*=\s*(\S+)/ig;
      $postinst_hook   = "$1"  if /^\s*postinst_hook\s*=\s*(\S+)/ig;
      $postrm_hook     = "$1"  if /^\s*postrm_hook\s*=\s*(\S+)/ig;
      $preinst_hook    = "$1"  if /^\s*preinst_hook\s*=\s*(\S+)/ig;
      $prerm_hook      = "$1"  if /^\s*prerm_hook\s*=\s*(\S+)/ig;
      $ramdisk         = "$1"  if /^\s*ramdisk\s*=\s*(.+)$/ig;
    }
    close CONF;
    $have_conffile = "Yes";
  }
}

if ($link_in_boot) {
  $image_dest = "/$image_dir/";
  $image_dest =~ s|^/*|/|o;
}

$image_dest = "$image_dest/";
$image_dest =~ s|/+$|/|o;

# The destdir may be gone by now.
if (-d "$image_dest") {
  chdir("$image_dest") or die "could not chdir to $image_dest:$!\n";
}

# Paranoid check to make sure that the correct value is put in there
if (! $kimage)                    {$kimage = "vmlinuz"} # Hmm. empty
elsif ($kimage =~ m/^b?uImage$/o) {$kimage = "vmlinuz"} # these produce vmlinuz
elsif ($kimage =~ m/^b?zImage$/o) {$kimage = "vmlinuz"} # these produce vmlinuz
elsif ($kimage =~ m/^[iI]mage$/o) { my $nop = $kimage;}
elsif ($kimage =~ m/^vmlinux$/o)  { my $nop = $kimage;}
else                              {$kimage = "vmlinuz"} # default

$ENV{KERNEL_ARCH}=$kernel_arch if $kernel_arch;


######################################################################
######################################################################
############
######################################################################
######################################################################
sub remove_sym_link {
  my $bad_image = $_[0];

  warn "Removing symbolic link $bad_image \n";
  if ($loader =~ /lilo/i) 
    {
      warn "Unless you used the optional flag in lilo, \n";
    }
  warn " you may need to re-run your boot loader" . ($loader ? "[$loader]":"")
    . "\n";
  # Remove the dangling link
  unlink "$bad_image";
}

######################################################################
######################################################################
############
######################################################################
######################################################################
sub CanonicalizePath {
  my $path = join '/', @_;
  my @work = split '/', $path;
  my @out;
  my $is_absolute;

  if (@work && $work[0] eq "") { $is_absolute = 1; shift @work; }

  while (@work) {
    my $seg = shift @work;
    if ($seg eq "." || $seg eq "") {
    } elsif ($seg eq "..") {
      if (@out && $out[-1] ne "..") {
        pop @out;
      } else {
        # Leading "..", or "../..", etc.
        push @out, $seg;
      }
    } else {
      push @out, $seg;
    }
  }

  unshift @out, "" if $is_absolute;
  return join('/', @out);
}

######################################################################
######################################################################
############
######################################################################
######################################################################
# This removes dangling symlinks. What do we do about hard links? Surely a 
# something with the nane $image_dest . "$kimage" ought not to be left behind? 
sub image_magic {
  my $kimage = $_[0];
  my $image_dest = $_[1];

  if (-l "$kimage") {
    # There is a symbolic link
    my $force_move = 0;
    my $vmlinuz_target = readlink "$kimage";
    my $real_target = '';
    $real_target = abs_path($vmlinuz_target) if defined ($vmlinuz_target);
    if (!defined($vmlinuz_target) || ! -f "$real_target") {
      # what, a dangling symlink?
      warn "The link "  . $image_dest . "$kimage is a damaged link\n";
      # Remove the dangling link
      &remove_sym_link("$kimage");
    }
    else {
      my $canonical_target = CanonicalizePath("$vmlinuz_target");
      if (! -e $canonical_target) {
	warn "The link "  . $image_dest . "$kimage is a dangling link\n";
	&remove_sym_link("$kimage");
      }
    }
  }
}

# set the env var stem
$ENV{'STEM'} = "linux";

sub exec_script {
  my $type   = shift;
  my $script = shift;
  print STDERR "Running $type hook script $script.\n";
  system ("$script $version $realimageloc$kimage-$version") &&
    print STDERR "User $type hook script [$script] ";
  if ($?) {
    if ($? == -1) {
      print STDERR "failed to execute: $!\n";
    }
    elsif ($? & 127) {
      printf STDERR "died with signal %d, %s coredump\n",
        ($? & 127),  ($? & 128) ? 'with' : 'without';
    }
    else {
      printf STDERR "exited with value %d\n", $? >> 8;
    }
  }
}
sub run_hook {
  my $type   = shift;
  my $script = shift;
  if ($script =~ m,^/,) {
    # Full path provided for the hook script
    if (-x "$script") {
      &exec_script($type,$script);
    }
    else {
      warn "The provided $type hook script [$script] could not be run.\n";
    }
  }
  else {
    # Look for it in a safe path
    for my $path ('/bin', '/sbin', '/usr/bin', '/usr/sbin') {
      if (-x "$path/$script") {
        &exec_script($type, "$path/$script");
        return 0;
      }
    }
    # No luck
    print STDERR "Could not find $type hook script [$script].\n";
    warn "Looked in: '/bin', '/sbin', '/usr/bin', '/usr/sbin'\n";
  }
}

## Run user hook script here, if any
if ($postrm_hook) {
  &run_hook("postrm", $postrm_hook);
}
if (-d "/etc/kernel/postrm.d") {
  warn "Examining /etc/kernel/postrm.d .\n";
  system ("run-parts --verbose --exit-on-error --arg=$version " .
          "--arg=$realimageloc$kimage-$version " .
          "/etc/kernel/postrm.d") &&
            die "Failed to process /etc/kernel/postrm.d";
}
if (-d "/etc/kernel/postrm.d/$version") {
  warn "Examining /etc/kernel/postrm.d/$version .\n";
  system ("run-parts --verbose --exit-on-error --arg=$version " .
          "--arg=$realimageloc$kimage-$version " .
          "/etc/kernel/postrm.d/$version") &&
            die "Failed to process /etc/kernel/postrm.d/$version";
}

# check and remove damaged and dangling symlinks
if ($ARGV[0] !~ /upgrade/) {
  system("$ramdisk -d -k " . $version . " > /dev/null 2>&1");
  if (-f $realimageloc . "initrd.img-$version.bak") {
    unlink $realimageloc . "initrd.img-$version.bak";
  }
  image_magic($kimage,          $image_dest);
  image_magic($kimage . ".old", $image_dest);
  image_magic("initrd.img",     $image_dest) if $initrd;
  image_magic("initrd.img.old", $image_dest) if $initrd;
}

exit 0;

__END__






